<template>
  <div class="alert-history">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>告警历史</span>
        </div>
      </template>
      
      <el-table :data="alerts" style="width: 100%">
        <el-table-column prop="triggered_at" label="触发时间" />
        <el-table-column prop="title" label="告警标题" />
        <el-table-column prop="severity" label="告警级别">
          <template #default="{ row }">
            <el-tag :type="getSeverityType(row.severity)" size="small">
              {{ getSeverityText(row.severity) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)" size="small">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="viewDetail(row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <el-dialog v-model="detailVisible" title="告警详情" width="600px">
        <div class="detail-item"><strong>标题：</strong>{{ currentAlert?.title || '-' }}</div>
        <div class="detail-item"><strong>层：</strong>{{ (currentAlert as any)?.layer || '-' }}</div>
        <div class="detail-item"><strong>表名：</strong>{{ (currentAlert as any)?.table_name || '-' }}</div>
        <div class="detail-item"><strong>级别：</strong>{{ getSeverityText((currentAlert as any)?.severity || '') }}</div>
        <div class="detail-item"><strong>状态：</strong>{{ getStatusText((currentAlert as any)?.status || '') }}</div>
        <div class="detail-item"><strong>触发时间：</strong>{{ currentAlert?.triggered_at || '-' }}</div>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="detailVisible = false">关闭</el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import type { AlertHistory } from '@/types'
import { alertApi } from '@/api/alert'
import { ElMessage } from 'element-plus'

const alerts = ref<AlertHistory[]>([])
const loading = ref(false)
const detailVisible = ref(false)
const currentAlert = ref<AlertHistory | null>(null)

const viewDetail = (alert: AlertHistory) => {
  currentAlert.value = alert
  detailVisible.value = true
}

const getSeverityType = (severity: string): string => {
  const types: Record<string, string> = {
    'LOW': 'info',
    'MEDIUM': 'warning',
    'HIGH': 'danger',
    'CRITICAL': 'danger'
  }
  return types[severity] || 'info'
}

const getSeverityText = (severity: string): string => {
  const texts: Record<string, string> = {
    'LOW': '低',
    'MEDIUM': '中',
    'HIGH': '高',
    'CRITICAL': '严重'
  }
  return texts[severity] || severity
}

const getStatusType = (status: string): string => {
  const types: Record<string, string> = {
    'ACTIVE': 'danger',
    'RESOLVED': 'success',
    'ACKNOWLEDGED': 'warning'
  }
  return types[status] || 'info'
}

const getStatusText = (status: string): string => {
  const texts: Record<string, string> = {
    'ACTIVE': '活跃',
    'RESOLVED': '已解决',
    'ACKNOWLEDGED': '已确认'
  }
  return texts[status] || status
}

const loadAlerts = async () => {
  loading.value = true
  try {
    const res = await alertApi.getHistory({ page: 1, size: 20 })
    alerts.value = res.data.items || []
  } catch (e) {
    console.error('Failed to load alert history:', e)
    ElMessage.error('加载告警历史失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadAlerts()
})
</script>

<style scoped>
.alert-history {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>

<style scoped>
.detail-item {
  margin: 8px 0;
}
.dialog-footer { display: flex; justify-content: flex-end; }
</style> 